package com.dmxh.controller;

import com.dmxh.result.Result;
import com.dmxh.service.CartService;
import com.dmxh.util.JwtUtil;
import com.dmxh.vo.CartlistVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @RequestMapping("/addCart")
    public Result addCart(@RequestHeader String adminToken, @RequestParam long itemId, @RequestParam Integer num) {
        Claims claims = JwtUtil.parseJwt(adminToken);
        String id = claims.getId();
        cartService.addCart(id,itemId,num);
        return Result.ok();
    }

    @GetMapping("/getCart")
    public Result getCart(@RequestHeader String adminToken) {
        Claims claims = JwtUtil.parseJwt(adminToken);
        String id = claims.getId();
        List<CartlistVo> list = cartService.getCart(id);
        return Result.ok().setData(list);
    }
}
